<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资格" prop="qualification">
        <el-input
          v-model="queryParams.qualification"
          placeholder="请输入资格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请性质" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择性质" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动类型" prop="pushType">
        <el-select v-model="queryParams.pushType" placeholder="请选择活动类型" clearable size="small">
          <el-option
            v-for="dict in pushTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="机构名称" prop="institutionName">
        <el-input
          v-model="queryParams.institutionName"
          placeholder="请输入机构名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['member:applypush:add']"
        >新增</el-button>
      </el-col>-->
     <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['member:applypush:edit']"
        >审核</el-button>
      </el-col>-->
     <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:applypush:remove']"
        >删除</el-button>
      </el-col>-->
     <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['member:applypush:export']"
        >导出</el-button>
      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="applypushList" @selection-change="handleSelectionChange">
  <!--    <el-table-column type="selection" width="55" align="center" />-->
    <!--  <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="资格" align="center" prop="qualification" />
      <el-table-column label="期限" align="center" prop="term" width="180">  </el-table-column>

        <el-table-column label="正面" align="center" prop="cardPositive" width="180">
          <template slot-scope="scope">
            <el-image :src="scope.row.cardPositive"  :preview-src-list="showImg(scope.row.cardPositive)"></el-image>
          </template>
      </el-table-column>
          <el-table-column label="反面" align="center" prop="term" width="180">
            <template slot-scope="scope">
              <el-image :src="scope.row.cardNegative"  :preview-src-list="showImg(scope.row.cardNegative)"></el-image>
            </template>
      </el-table-column>
       <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.term) }}</span>
        </template>-->

      <el-table-column label="申请性质" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="活动类型" align="center" prop="pushType" :formatter="pushTypeFormat" />
      <el-table-column label="机构名称" align="center" prop="institutionName" />
      <el-table-column label="审核状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,1)"
            v-hasPermi="['member:applypush:edit']"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,2)"
            v-hasPermi="['member:applypush:edit']"
          >失败</el-button>
        <!--  <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['member:applypush:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改申请发布活动资格对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" :disabled="editflag"/>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话"  :disabled="editflag" />
        </el-form-item>
        <el-form-item label="资格" prop="qualification">
          <el-input v-model="form.qualification" placeholder="请输入资格"  :disabled="editflag"/>
        </el-form-item>
        <el-form-item label="期限" prop="term">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.term"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择期限"
                          :disabled="editflag">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请性质">
          <el-select v-model="form.type" placeholder="请选择申请性质"   :disabled="editflag">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"

            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动类型">
          <el-select v-model="form.pushType" placeholder="请选择活动类型"   :disabled="editflag">
            <el-option
              v-for="dict in pushTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"

            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机构名称" prop="institutionName">
          <el-input v-model="form.institutionName" placeholder="请输入机构名称"  :disabled="editflag"/>
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="form.status" placeholder="请选择审核状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
       <!-- <el-form-item label="备用字段1" prop="bak1">
          <el-input v-model="form.bak1" placeholder="请输入备用字段1" />
        </el-form-item>
        <el-form-item label="备用字段2" prop="bak2">
          <el-input v-model="form.bak2" placeholder="请输入备用字段2" />
        </el-form-item>
        <el-form-item label="备用字段3" prop="bak3">
          <el-input v-model="form.bak3" placeholder="请输入备用字段3" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listApplypush, getApplypush, delApplypush, addApplypush, updateApplypush, exportApplypush } from "@/api/member/applypush";
  import defaultSettings from '@/settings'
  export default {
    name: "Applypush",
    data() {
      return {
        imgpre: defaultSettings.ossPreview,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 申请发布活动资格表格数据
        applypushList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 机构，个人字典
        typeOptions: [],
        // 0活动，1助力字典
        pushTypeOptions: [],
        // 审核通过、不通过字典
        statusOptions: [],
        editflag:false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          phone: undefined,
          qualification: undefined,
          type: undefined,
          pushType: undefined,
          institutionName: undefined,
          status: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            { required: true, message: "姓名不能为空", trigger: "blur" }
          ],
          phone: [
            { required: true, message: "电话不能为空", trigger: "blur" }
          ],
          qualification: [
            { required: true, message: "资格不能为空", trigger: "blur" }
          ],
          type: [
            { required: true, message: "机构，个人不能为空", trigger: "blur" }
          ],
          pushType: [
            { required: true, message: "0活动，1助力不能为空", trigger: "blur" }
          ],
          status: [
            { required: true, message: "审核通过、不通过不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("apply_push_type").then(response => {
        this.typeOptions = response.data;
      });
      this.getDicts("user_apply_type").then(response => {
        this.pushTypeOptions = response.data;
      });
      this.getDicts("apply_push_type_state").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      showImg(e) {
        let array = [];
        console.log(e);
        array.push(e);
        /*   if (e) {
           e.forEach((item) => {
             array.push(this.imgpre+item.imgPath);

           })
         }*/
        return array;
      },
      /** 查询申请发布活动资格列表 */
      getList() {
        this.loading = true;
        listApplypush(this.queryParams).then(response => {
          this.applypushList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 机构，个人字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type);
      },
      // 0活动，1助力字典翻译
      pushTypeFormat(row, column) {
        return this.selectDictLabel(this.pushTypeOptions, row.pushType);
      },
      // 审核通过、不通过字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          phone: undefined,
          qualification: undefined,
          term: undefined,
          type: undefined,
          pushType: undefined,
          institutionName: undefined,
          status: undefined,
          bak1: undefined,
          bak2: undefined,
          bak3: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.editflag=false;
        this.open = true;
        this.title = "添加申请发布活动资格";
      },
      /** 修改按钮操作 */
      handleUpdate(row,e) {
        this.reset();
        this.editflag=true;
        const id = row.id
        this.form.id=id;
        this.form.status=e
        updateApplypush(this.form).then(response => {
          if (response.code === 200) {
            this.msgSuccess("修改成功");
            this.getList();
          }
        });
      },
      pass(row,e) {
        this.reset();
        this.editflag=true;
        const ids =  this.ids;
       for(var i=0;i<ids.length;i++){
         updateApplypush(this.form).then(response => {
           if (response.code === 200) {
             this.msgSuccess("修改成功");
             this.getList();
           }
         });
       }

      },
      nopass(row,e) {
        this.reset();
        this.editflag=true;
        const id = row.id
        this.form.id=id;
        this.form.status=e
        updateApplypush(this.form).then(response => {
          if (response.code === 200) {
            this.msgSuccess("修改成功");
            this.getList();
          }
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateApplypush(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addApplypush(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除申请发布活动资格编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delApplypush(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有申请发布活动资格数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportApplypush(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      }
    }
  };
</script>
